package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.SystemDictionaryItem;
import cn.wolfcode.rbac.query.SystemDictionaryItemQuery;
import cn.wolfcode.rbac.service.ISystemDictionaryItemService;
import cn.wolfcode.rbac.service.ISystemDictionaryService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    //@RequiredPermission(name="部门列表")
    // qo 要贴注解，@ModelAttribute 将数据添加到模型对象中，等价于 model.addAttribute("attributeName", qo);
    public String list(Model model,@ModelAttribute("qo") SystemDictionaryItemQuery qo){
        PageInfo result = systemDictionaryItemService.query(qo);
        model.addAttribute("result", result);

        // 查询所有的字典
        model.addAttribute("systemdictionarys",systemDictionaryService.listAll());
        return "systemDictionaryItem/list"; // /WEB-INF/views/systemDictionaryItem/list3.ftl
    }

 /*   @RequestMapping("/input")
    @RequiredPermission(name="部门编辑页面")
    public String input(Long id, Model model){
        if(id != null){ // 点击了修改按钮过来
            SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.get(id);
            model.addAttribute("systemDictionaryItem", systemDictionaryItem);
        }
        return "/WEB-INF/views/systemDictionaryItem/input.jsp"; // /WEB-INF/views/systemDictionaryItem/input.jsp
    }*/


    @RequestMapping("/saveOrUpdate")
    @ResponseBody   // 告诉 Spring MVC，我们需要转为 json格式返回给前端
   // @RequiredPermission(name="部门保存或修改")
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        try {
            if(systemDictionaryItem.getId() == null){ // 代表新增
                systemDictionaryItemService.save(systemDictionaryItem);
            }else { // 代表修改
                systemDictionaryItemService.update(systemDictionaryItem);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
        //return "redirect:/systemDictionaryItem/list.do";  // http://localhost/systemDictionaryItem/list.do
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        // int i = 1/0;
        try {
            if (id != null) {
                systemDictionaryItemService.delete(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败！");
        }
        // return "redirect:/systemDictionaryItem/list.do";  // http://localhost/systemDictionaryItem/list.do
    }
}
